package com.zhihaoliang.baseframe;

import com.zhihaoliang.baseframe.db.entity.CommentEntity;
import com.zhihaoliang.baseframe.db.entity.ProductEntity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 创建日期：2020-02-25
 * 描述:用于数据的生成
 * 作者:支豪亮
 */
public class DataGenerator {
    private static final String[] FIRST = new String[]{
            "Special edition", "New", "Cheap", "Quality", "Used"};
    private static final String[] SECOND = new String[]{
            "Three-headed Monkey", "Rubber Chicken", "Pint of Grog", "Monocle"};
    private static final String[] DESCRIPTION = new String[]{
            "is finally here", "is recommended by Stan S. Stanman",
            "is the best sold product on Mêlée Island", "is \uD83D\uDCAF", "is ❤️", "is fine"};
    private static final String[] COMMENTS = new String[]{
            "Comment 1", "Comment 2", "Comment 3", "Comment 4", "Comment 5", "Comment 6"};

    private static final Random RANDOM = new Random();


    public static List<ProductEntity> generatorProducts() {
        List<ProductEntity> products = new ArrayList<>(FIRST.length * SECOND.length);
        for (int i = 0; i < FIRST.length; i++) {
            for (int j = 0; j < SECOND.length; j++) {
                ProductEntity productEntity = new ProductEntity();
                productEntity.setName(FIRST[i] + " " + SECOND[j]);
                productEntity.setDescription(productEntity.getName() + " " + DESCRIPTION[j]);
                productEntity.setPrice(RANDOM.nextInt(240));
                productEntity.setId(FIRST.length * i + j);
                products.add(productEntity);
            }
        }
        return products;
    }

    public static List<CommentEntity> generatorComments(List<ProductEntity> products) {
        List<CommentEntity> comments = new ArrayList<>();

        for (ProductEntity product : products) {
            int commentNum = RANDOM.nextInt(5) + 1;
            for (int i = 0; i < commentNum; i++) {
                CommentEntity comment = new CommentEntity();
                comment.setProductId(product.getId());
                comment.setText(COMMENTS[i]+ " for "+product.getName());

                long postAtTime =System.currentTimeMillis() -
                        TimeUnit.DAYS.toMillis(commentNum -1L)+TimeUnit.HOURS.toMillis(i);
                comment.setPostAt(new Date(postAtTime));

                comments.add(comment);
            }
        }

        return comments;
    }
}
